package com.wohuijituan.whcsmerchant.activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.bean.CommonBean;
import com.wohuijituan.whcsmerchant.callback.CommonCallBack;

import com.wohuijituan.whcsmerchant.url.ParamsKey;
import com.wohuijituan.whcsmerchant.url.Urls;
import com.wohuijituan.whcsmerchant.utils.LogUtils;
import com.wohuijituan.whcsmerchant.utils.MyKey;
import com.wohuijituan.whcsmerchant.utils.MyOkHttpUtils;
import com.wohuijituan.whcsmerchant.utils.MyRequestParams;




import okhttp3.Call;


/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/26 10:30
 * 版本：V1.0
 * 修改历史：
 */
public class LoginActivity extends MyBaseActivity implements View.OnClickListener{
    private static final String TAG = "LoginActivity";
    private EditText etInputAccount;
    private EditText etInputPassword;
    private Button btnLogin;
    private String strUser;
    private String strPwd;
    private MyOkHttpUtils myOkHttpUtils;
    private MyRequestParams params;
    private Context ctx;
    private void assignViews() {
        etInputAccount = (EditText) findViewById(R.id.et_input_account);
        etInputPassword = (EditText) findViewById(R.id.et_input_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        assignViews();
        initData();
        initListener();
    }
    private void initData() {
        myOkHttpUtils = new MyOkHttpUtils();
        params = new MyRequestParams();
        ctx = this;
    }
    private void initListener() {
        btnLogin.setOnClickListener(this);
    }

    /**
     * 存储用户数据
     */
    private void saveUserData(String data) {
        SharedPreferences sp = getSharedPreferences(MyKey.userConfig, MODE_PRIVATE);
        sp.edit().putString("userKey", data)
                .commit();
        myApplication.setUserKey(data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                strUser = etInputAccount.getText().toString().trim();
                strPwd = etInputPassword.getText().toString().trim();
                if (strUser == null || strUser.equals("")) {
                    etInputAccount.setError("用户名不能为空");
                    break;
                }
                if (strPwd == null || strPwd.equals("")) {
                    etInputPassword.setError("密码不能为空");
                    break;
                }
                LogUtils.e(TAG, strPwd + "**" + strUser);
                if (myApplication.isNetworkAvailable(ctx)){
                    LogUtils.e(TAG,"网络可用");
                    doLogin(strUser, strPwd,"0.0.0.0" ,myApplication.getUserModel());//myApplication.getLocalIpAddress()
                }
                break;
        }
    }

    /**
     * 进行网络请求
     * @param user 用户账号
     * @param pwd 用户密码
     * @param ip 当前用户ip
     */
    private void doLogin(String user,String pwd,String ip,String model) {
        showDialog(this);
        params.clear();
        params.addStringRequest(ParamsKey.Login_User, user);
        params.addStringRequest(ParamsKey.Login_Password,pwd);
        params.addStringRequest(ParamsKey.Login_IP,ip);
        params.addStringRequest(ParamsKey.Login_Model,model);
        myOkHttpUtils.postRequest(Urls.Login, params, new CommonCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dismissDialog();
                showToast(LoginActivity.this, "登录失败");
                LogUtils.e(TAG,"错误信息:"+e.toString());
                goBack();
            }
            @Override
            public void onResponse(CommonBean response, int id) {
                if (response.getState() == 1) {
                    saveUserData(response.getData());
                    goBack();
                } else {
                    showToast(LoginActivity.this, response.getMsg() + "*");
                }
                dismissDialog();
            }
        });
    }
    /**
     * 跳转到主页,或者返回原先页面
     */
    private void goBack(){
        Intent intent = new Intent(this,HomeActivity.class);
        startActivity(intent);
        finish();
    }
}
